//
//  MyEnrollViewController.swift
//  DriverSchoolAPP
//
//  Created by Logel on 2017/5/18.
//  Copyright © 2017年 Logel. All rights reserved.
//

import UIKit
var oList = [Order]()
class MyEnrollViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,LgCallBack,CGYJSON{
    @IBOutlet weak var myTableView: UITableView!
    var placeId = ""
    @IBOutlet weak var placeName: UILabel!
    var placeN = ""
    var destOrder = PreOrder()
    override func viewDidLoad() {
        super.viewDidLoad()
        placeName.text = placeN
        // Do any additional setup after loading the view.
    }
    
    override func viewDidAppear(_ animated: Bool) {
        myTableView.reloadData()
        
        for x in oList {
            print(x)
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch oList.count {
        case 4:
            return oList.count
        default:
            return 1+oList.count
        }
        
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        switch oList.count {
        case 4:
            let cell = tableView.dequeueReusableCell(withIdentifier: "reuseCell2", for: indexPath) as! NormalStudentTableViewCell
            cell.o = oList[indexPath.row]
            return cell
        default:
            switch indexPath.row {
            case 0:
                let cell = tableView.dequeueReusableCell(withIdentifier: "reuseCell1", for: indexPath) as! addStudentTableViewCell
                return cell
            default:
                let cell = tableView.dequeueReusableCell(withIdentifier: "reuseCell2", for: indexPath) as! NormalStudentTableViewCell
                cell.o = oList[indexPath.row - 1]
                return cell
            }

        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch oList.count {
        case 4:
            oList.remove(at: indexPath.row)
            myTableView.reloadData()
        default:
            switch indexPath.row {
            case 0:
                self.performSegue(withIdentifier: "toEnrollDetail", sender: nil)
            default:
                oList.remove(at: indexPath.row - 1)
                myTableView.reloadData()
            }
        }
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        switch oList.count {
        case 4:
            return 100
        default:
            switch indexPath.row {
            case 0:
                return 80
            default:
                return 100
            }
        }
    }

    @IBAction func goToOrder(_ sender: UIButton) {
        asyn_req_post(method: M_OrderPreSave, cb: self, p: ["placeId":placeId as AnyObject ,"accountId":user.accountId as AnyObject,"orders":oList.toJSONString() as AnyObject])
       

     
    }
    
    func callBack(method: String, data: AnyObject) {
        let j = JSON(data)
        print(j)
        let head = parseMobileHead(x: j["MobileHead"])
        if head.code == "1" {
            destOrder = parsePreOrder(x: j["MobileBody"])
            self.performSegue(withIdentifier: "toOrder", sender: nil)
        }else{
            showMsg(msg: head.message)
        }
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "toOrder" {
            let dest = segue.destination as! MakeSureViewController
            dest.perOrder = destOrder
        }
    }
}
